package com.example.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.Account;
import com.example.entity.PlaylistItem;
import com.example.entity.Sing;
import com.example.exception.CustomException;
import com.example.mapper.PlaylistItemMapper;
import com.example.mapper.SingMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务处理
 **/
@Service
public class PlaylistItemService {

    @Resource
    private PlaylistItemMapper playlistItemMapper;
    @Resource
    private SingMapper singMapper;

    /**
     * 新增
     */
    public void add(PlaylistItem playlistItem) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (ObjectUtil.isNull(currentUser)) {
            throw new CustomException(ResultCodeEnum.TOKEN_CHECK_ERROR);
        }
        playlistItem.setUserId(currentUser.getId());
        List<PlaylistItem> playlistItems = playlistItemMapper.selectAll(playlistItem);
        if (CollectionUtil.isNotEmpty(playlistItems)) {
            throw new CustomException("500", "该歌曲已经加入过该歌单，请勿重复加入");
        }
        playlistItemMapper.insert(playlistItem);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        playlistItemMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            playlistItemMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(PlaylistItem playlistItem) {
        playlistItemMapper.updateById(playlistItem);
    }

    /**
     * 根据ID查询
     */
    public PlaylistItem selectById(Integer id) {
        return playlistItemMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<PlaylistItem> selectAll(PlaylistItem playlistItem) {
        return playlistItemMapper.selectAll(playlistItem);
    }

    /**
     * 分页查询
     */
    public PageInfo<PlaylistItem> selectPage(PlaylistItem playlistItem, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PlaylistItem> list = playlistItemMapper.selectAll(playlistItem);
        return PageInfo.of(list);
    }

    public List<Sing> selectAllSongs(PlaylistItem playlistItem) {
        List<PlaylistItem> playlistItems = playlistItemMapper.selectAll(playlistItem);
        List<Sing> list = new ArrayList<>();
        for (PlaylistItem item : playlistItems) {
            Sing sing = singMapper.selectById(item.getSongId());
            if (ObjectUtil.isNotEmpty(sing)) {
                list.add(sing);
            }
        }
        return list;
    }

    public void deleteSong(PlaylistItem playlistItem) {
        List<PlaylistItem> list = playlistItemMapper.selectAll(playlistItem);
        if (CollectionUtil.isNotEmpty(list)) {
            playlistItemMapper.deleteById(list.get(0).getId());
        }
    }
}